package com.lwc.cfdns.controller.admin.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lwc.cfdns.annotation.OperLog;
import com.lwc.cfdns.constants.ConstantsUrl;
import com.lwc.cfdns.pojo.bo.system.ConfigBO;
import com.lwc.cfdns.pojo.dto.ValidGroup;
import com.lwc.cfdns.pojo.dto.system.ConfigDTO;
import com.lwc.cfdns.pojo.dto.system.ConfigSetValueDTO;
import com.lwc.cfdns.pojo.entity.SystemConfig;
import com.lwc.cfdns.pojo.vo.system.ConfigVO;
import com.lwc.cfdns.service.impl.SystemConfigServiceImpl;
import com.lwc.cfdns.utils.ConvertUtils;
import com.lwc.cfdns.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;

/**
 * 参数配置
 *
 * @author luwc
 * @title ConfigController
 * @description
 * @time 2023/6/1 09:26:11
 * @version: 1.0
 */
@RestController
@RequestMapping(ConstantsUrl.ADMIN + "/system")
public class ConfigController {
    @Autowired
    private SystemConfigServiceImpl systemConfigService;

    /**
     * 创建
     *
     * @param configDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/1 09:29:56
     */
    @PreAuthorize("hasAnyAuthority('system:config:create')")
    @PostMapping("/config")
    public Map<String, Object> create(@Validated(ValidGroup.Create.class) @RequestBody ConfigDTO configDTO) {
        return systemConfigService.create(configDTO);
    }

    /**
     * 修改
     *
     * @param configDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/1 09:29:56
     */
    @PreAuthorize("hasAnyAuthority('system:config:update')")
    @PutMapping("/config")
    public Map<String, Object> update(@Validated(ValidGroup.Update.class) @RequestBody ConfigDTO configDTO) {
        return systemConfigService.update(configDTO);
    }

    /**
     * 更新配置值
     *
     * @param configSetValueDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/1 09:29:56
     */
    @PreAuthorize("hasAnyAuthority('system:config:setValue')")
    @PutMapping("/config/setValue")
    public Map<String, Object> setValue(@Validated(ValidGroup.Update.class) @RequestBody ConfigSetValueDTO configSetValueDTO) {
        return systemConfigService.setValue(configSetValueDTO);
    }

    /**
     * 详情
     *
     * @param id
     * @return ConfigVO
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/1 09:35:55
     */
    @PreAuthorize("hasAnyAuthority('system:config:view')")
    @GetMapping("/config/{id}")
    public ConfigVO view(@PathVariable("id") @NotNull(message = "ID不能为空") Long id) {
        return systemConfigService.view(id);
    }

    /**
     * 删除
     *
     * @param ids
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/1 09:39:13
     */
    @PreAuthorize("hasAnyAuthority('system:config:delete')")
    @DeleteMapping("/config/{ids}")
    public Map<String, Object> delete(@PathVariable("ids") @NotBlank(message = "ID不能为空") String ids) {
        return systemConfigService.delete(ids);
    }

    /**
     * 列表
     *
     * @param configBO
     * @return IPage<SystemConfig>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/1 09:49:58
     */
    @PreAuthorize("hasAnyAuthority('system:config:lists')")
    @GetMapping("/config")
    public IPage<SystemConfig> lists(ConfigBO configBO) {
        return systemConfigService.lists(configBO);
    }

    /**
     * 删除所有配置缓存
     *
     * @param
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/29 15:46:18
     */
    @PreAuthorize("hasAnyAuthority('system:config:refreshCache')")
    @DeleteMapping("/config/refreshCache")
    public Map<String, Object> refreshCache() {
        return systemConfigService.refreshCache();
    }

    /**
     * 获取配置
     *
     * @param enName
     * @return String
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/1 13:43:08
     */
    @GetMapping("/config/key/{enName}")
    public Map<String, Object> viewByEnNameOfCache(@PathVariable("enName") @NotBlank(message = "配置键名不能为空") String enName) {
        Map<String, Object> map = new HashMap<>();
        map.put("value", systemConfigService.viewByEnNameOfCache(enName).getValue());
        return map;
    }
}
